<template>
  <div>
    <a-alert
      type="error"
      :message="`有${errorDataCount}条数据异常，请尽快处理`"
      v-if="alertiVisible"
      show-icon
      closable
      :after-close="handleClose"
      close-text="查看详情"
      style="margin: 24px; background-color: #fff1f0"
    />
    <div class="xq-form-wrapper" style="margin: 24px">
      <a-spin :spinning="spinning" class="xq-form-content">
        <XqTable
          :customize="FormJson['config']['table']['customize']"
          :scroll="{ x: columns.length * 70 }"
          :pagination="false"
          :dataSource="tableData"
          :columns="columns"
        >
          <template v-for="(col, index) in columns" v-slot:[`xq_${col.dataIndex}`]>
            <span :key="index">
              {{ col.customTitle }}
            </span>
          </template>
          <!-- eslint-disable-next-line prettier/prettier -->
          <template v-for="(col, index) in columns" v-slot:[col.dataIndex]="text, record">
            <span v-if="col.dataIndex === 'positionCount'" :key="index"> 导入 </span>
            <div v-else-if="col.dataIndex === 'skill'" :key="index" class="tags">
              <div v-for="(item, index) in record.skill" :key="index">{{ item }}</div>
            </div>
            <XqTableCell v-else :key="index" :record="record" :field="FormJson['fields'][col.dataIndex]" :dynamicData="dynamicData" />
          </template>
        </XqTable>
        <div class="xq-form-content-pagination">
          <div class="xq-form-content-pagination-total">共 {{ total }} 条</div>
          <a-pagination
            v-model="pageNo"
            :page-size="pageSize"
            :total="total"
            :show-size-changer="true"
            :show-quick-jumper="true"
            :page-size-options="['10', '20', '50', '100']"
            @change="onPageChange"
            @showSizeChange="onPageSizeChange"
          />
        </div>
      </a-spin>
    </div>
    <div class="preview-btn">
      <a-button style="margin-right: 12px" @click="$emit('onAction', 0)">返回</a-button>
      <a-button type="primary" @click="onSubmit" :disabled="errorDataCount > 0"> 提交 </a-button>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import FormJson from "./FormJson";
import XqCurdMixin from "xqjr-plugin-form/mixins/XqCurdMixin";
import { getTalentPoolImportInfo } from "../../api";

const curdMixin = new XqCurdMixin({
  FormJson,
  tableConfigKey: "table"
});

export default {
  mixins: [curdMixin],
  props: {
    talentPoolTempId: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      alertiVisible: false,
      spinning: false,
      tableData: [],
      total: 0,
      pageNo: 1,
      pageSize: 20,
      errorDataCount: 0
    };
  },
  created() {
    this.onGetTalentPoolImportInfo();
  },
  methods: {
    async onGetTalentPoolImportInfo() {
      try {
        this.spinning = true;
        const { data } = await getTalentPoolImportInfo({
          talentPoolRelation: this.talentPoolTempId,
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          "orders[0].property": "gmtCreate",
          "orders[0].desc": true
        });
        this.tableData = data.allData;
        this.tableData.forEach(e => {
          if (e.skill) {
            e.skill = e.skill.split(",");
          } else {
            e.skill = [];
          }
        });
        this.total = data.allDataCount;
        this.errorDataCount = data.errorDataCount;
        if (data.errorDataCount > 0) this.alertiVisible = true;
        setTimeout(() => (this.spinning = false), 500);
      } catch (err) {
        console.log("error log --> ", err);
        setTimeout(() => (this.spinning = false), 500);
      }
    },
    onPageChange(page) {
      this.pageNo = page;
      this.onGetTalentPoolImportInfo();
    },
    onPageSizeChange(current, pageSize) {
      this.pageNo = 1;
      this.pageSize = pageSize;
      this.onGetTalentPoolImportInfo();
    },
    handleClose() {
      this.alertiVisible = false;
      this.$router.push({
        name: "talentAbnormal",
        query: { talentPoolTempId: this.talentPoolTempId }
      });
    },
    async onSubmit() {
      axios({
        url: `/recruit2/api/v1/talentPoolTemps/toSave/${this.talentPoolTempId}`,
        method: "get",
        headers: {
          token: this.$store.state.auth.token
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.$emit("onActionS", 2, true, res.data.data);
        } else {
          this.$emit("onActionS", 2, false, res.data.msg);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.preview-btn {
  text-align: center;
}
.tags {
  display: flex;
  div {
    padding: 0 8px;
    height: 22px;
    line-height: 22px;
    margin-right: 8px;
    background: rgba(0, 0, 0, 0.04);
    border-radius: 2px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    box-sizing: border-box;
  }
}
</style>
